package com.kaiyiweitong.tongxing.activity.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.adapter.IntegralItemAdapter;
import com.kaiyiweitong.tongxing.adapter.IntegralTypeAdapter;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.bean.IntegralItemResData;
import com.kaiyiweitong.tongxing.bean.IntegralTypeResData;
import com.kaiyiweitong.tongxing.constant.HttpConstant;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/17 0017.
 */

public class IntegralShopActivity extends BaseActivity {
    private List<IntegralTypeResData> mTypeData = new ArrayList<>();
    private IntegralTypeAdapter mClassifyVerAdapter;
    private List<IntegralItemResData> mItemData = new ArrayList<>();
    private IntegralItemAdapter mIntegralItemAdapter;
    private RecyclerView mRv_integral_type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_shop);
        init();
        initTypeData();
    }

    private void init() {
        mRv_integral_type = (RecyclerView) findViewById(R.id.rv_integral_type);
        mRv_integral_type.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.VERTICAL, false));
        mClassifyVerAdapter = new IntegralTypeAdapter(this, mTypeData, mListener, mCurrentPosition);
        mRv_integral_type.setAdapter(mClassifyVerAdapter);


        RecyclerView rv_integral_item = (RecyclerView) findViewById(R.id.rv_integral_item);
        rv_integral_item.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.VERTICAL, false));
        mIntegralItemAdapter = new IntegralItemAdapter(this, mItemData, mItemListener);
        rv_integral_item.setAdapter(mIntegralItemAdapter);
    }

    private void initTypeData() {
        HttpUtil.get(HttpConstant.INTEGRAL_SHOP_CLASSIFY, null, this, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                if (CommonUtil.isNotEmpty(result)) {
                    List<IntegralTypeResData> data = HttpUtil.checkArrayResponse(IntegralShopActivity.this, result, IntegralTypeResData.class);
                    if (data != null) {
                        mTypeData.clear();
                        mTypeData.addAll(data);
                        showItemData(0);
                    }
                }
            }
        });
    }

    private IntegralTypeAdapter.OnClickListener mListener = new IntegralTypeAdapter.OnClickListener() {
        @Override
        public void onClick(int position) {
            showItemData(position);
        }
    };

    private int mCurrentPosition = -1;

    private void showItemData(int position) {
        if (position == mCurrentPosition) {
            return;
        }
        mCurrentPosition = position;
        mClassifyVerAdapter = new IntegralTypeAdapter(this, mTypeData, mListener, mCurrentPosition);
        mRv_integral_type.setAdapter(mClassifyVerAdapter);


        Map<String, String> map = new HashMap<>();
        map.put("id", mTypeData.get(position).getIntegral_class_id());
        HttpUtil.get(HttpConstant.INTEGRAL_SHOP_ITEM, map, this, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                if (CommonUtil.isNotEmpty(result)) {
                    List<IntegralItemResData> data = HttpUtil.checkArrayResponse(IntegralShopActivity.this, result, IntegralItemResData.class);
                    if (data != null) {
                        mItemData.clear();
                        mItemData.addAll(data);
                        mIntegralItemAdapter.notifyDataSetChanged();
                    } else {
                        mItemData.clear();
                        mIntegralItemAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    private IntegralItemAdapter.OnclickListener mItemListener = new IntegralItemAdapter.OnclickListener() {
        @Override
        public void onclick(String id) {

        }
    };
}
